package com.ruoyi.merchant.service.impl;

import com.ruoyi.merchant.mapper.GroupMapper;
import com.ruoyi.merchant.service.MineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class MineServiceImpl implements MineService {

    @Autowired
    private GroupMapper groupMapper;
    // 拼团 数据
    @Override
    public Map<String, List> getMenberReport() {
        List<String> monthList = new ArrayList<>();
        List<Integer> memberCount = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");

        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.YEAR, -1); // 获取过去一年的数据
        for (int i = 0; i < 12; i++) {
            calendar.add(Calendar.MONTH, 1);
            String date = sdf.format(calendar.getTime());
            monthList.add(date);
            // 查询该月的拼团数量
            Integer count = groupMapper.findCountByDate(date);
            memberCount.add(count);
        }
        Map<String, List> data = new HashMap<>();
        data.put("monthList", monthList);
        data.put("memberCount", memberCount);

        return data;
    }

    // 销售额 数据表
    @Override
    public Map<String, List> getSalesReport(Integer lid) {
        List<String> monthList = new ArrayList<>();
        List<BigDecimal> salesAmount = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");

        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.YEAR, -1); // 获取过去一年的数据

        for (int i = 0; i < 12; i++) {
            calendar.add(Calendar.MONTH, 1);
            String date = sdf.format(calendar.getTime());
            monthList.add(date);

            // 查询该月的销售额
            BigDecimal sales = groupMapper.findSalesByDate(date,lid);
            salesAmount.add(sales);
        }

        Map<String, List> data = new HashMap<>();
        data.put("monthList", monthList);
        data.put("salesAmount", salesAmount);

        return data;
    }


    // 客户 数据表
    @Override
    public Map<String, List> getCustomerReport(Integer lid) {
        List<String> monthList = new ArrayList<>();
        List<Integer> customerCount = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");

        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.YEAR, -1); // 获取过去一年的数据
        for (int i = 0; i < 12; i++) {
            calendar.add(Calendar.MONTH, 1);
            String date = sdf.format(calendar.getTime());
            monthList.add(date);

            // 查询该月的客户数量
            Integer count = groupMapper.findCustomerCountByDate(date,lid);
            customerCount.add(count);
        }
        Map<String, List> data = new HashMap<>();
        data.put("monthList", monthList);
        data.put("customerCount", customerCount);

        return data;
    }

}
